import axios from "axios";

const baseUrl = "/api";
const timeout = 10000;

export function getData(url, params, noToken) {
    return axios.get(baseUrl + url, {
        params: params,
        headers: {
            "token": noToken === true ? null : "Bearer " + localStorage.getItem("userInfo")
        }
    }).then(res => {
        return res.data;
    });
}

export function deleteData(url) {
    return axios.delete(baseUrl + url).then(res => res.data);
}

export function putData(url, params, noToken) {
    return upDataBase("put", url, params, noToken);
}

export function postData(url, params, noToken) {
    return upDataBase("post", url, params, noToken);
}

export const upDataBase = (method, url, params, noToken) => {
    let formData = new FormData();
    for (const formKey in params) {
        formData.append(formKey, params[formKey]);
    }
    return axios({
        method: method,
        url: baseUrl + url,
        data: formData,
        timeout: timeout,
        headers: {
            "token": noToken === true ? null : "Bearer " + localStorage.getItem("userInfo")
        }
    }).then(res => res.data);
};

export const Utils = {
    /**
     * 从sessionStorage中取出对象并且进行深度解析
     */
    parseStorage: function(key) {
        return this.toObject(JSON.parse(sessionStorage.getItem(key)));
    },
    putStorage: (key, value) => {
        sessionStorage.setItem(key, JSON.stringify(value));
    },
    parseLocalStorage: (key) => {
        return this.toObject(JSON.parse(localStorage.getItem(key)));
    },
    putLocalStorage: (key, value) => {
        localStorage.setItem(key, value);
    },
    /**
     * 接收一个对象, 解析内部字符串的所有内容
     */
    toObject: function(obj) {
        if (typeof obj == "string") {
            // 判断是否以 [,{ 开头或以 ],}结尾
            if (obj.match(/^[\x7B\x5B].*[\x7D\x5D]$/)) {
                // 解析对象
                obj = JSON.parse(obj);
                if (Array.isArray(obj)) {
                    // console.log(parseKey,"::key解析数组元素::", obj[parseKey]);
                    for (let i = obj.length - 1; i >= 0; i--) {
                        obj[i] = this.toObject(obj[i]);
                    }
                    return obj;
                }
            }
        }else{
            for (const objKey in obj) {
                obj[objKey] = this.toObject(obj[objKey]);
            }
        }
        
       /* for (const parseKey in obj) {
            // console.log("key索引::", parseKey);
            // 判断类型
            if (typeof obj[parseKey] == "string") {
                // console.log("解析元素::", obj[parseKey]);
                // 匹配正则
                if (obj[parseKey].match(/^[\x7B\x5B].*[\x7D\x5D]$/)) {
                    // console.log("开始解析元素::", obj[parseKey]);
                    // 解析元素 递归解析
                    obj[parseKey] = this.toObject(JSON.parse(obj[parseKey]));
                    // console.log("解析后::", obj[parseKey]);
                    if (Array.isArray(obj[parseKey])) {
                        // console.log(parseKey,"::key解析数组元素::", obj[parseKey]);
                        for (let i = obj[parseKey].length - 1; i >= 0; i--) {
                            obj[parseKey][i] = this.toObject(obj[parseKey][i]);
                        }
                    }
                }
            }
        }*/
        return obj;
    },
    copyObject(provide) {
        return this.toObject(JSON.stringify(provide));
    }
    
};
